package com.O1DesignModel;

/**
 * 抽象工厂模式 ：
 *  我现在需要一个工厂，生产食物和饮料，如何实现呢？
 *   1.首先可以设置两个工厂 分别可以生产食物和饮料，食物是一个简单工厂，饮料也是一个饮料工厂，
 *   2.总的工厂是一个抽象的工厂，有两个抽象方法 分别可以创建食物工厂和饮料工厂
 *   3.如何创建食物或者饮料工厂呢？这里就需要一个工厂生成累  FactoryProducer 里面有一个抽象的静态方法  getFactory 生成对应的工厂
 *
 *
 *   */
interface Drinks{
    public void drink();
}

interface Food{
    public void eat();
}

class Tea implements Drinks{
    public void drink(){
        System.out.println("我是茶叶");
    }
}

class Cola implements Drinks{
    public void drink(){
        System.out.println("我是可乐");
    }
}


class Xiaomi implements Food{
    public void eat(){
        System.out.println("我是小米");
    }
}

class Yanmai implements Food{
    public void eat(){
        System.out.println("我是燕麦");
    }
}
abstract class  Factory{
    public abstract Drinks getDrinks(String type);
    public abstract Food getFood(String type);
}

class FoodFactory extends Factory{

    public Drinks getDrinks(String type) {
        return null;
    }

    @Override
    public Food getFood(String type) {
        if(type.equals("xiaomi")){
            return new Xiaomi();
        }else if(type.equals("yanmai")){
            return new Yanmai();
        }else {
            throw new RuntimeException("no such food");
        }
    }
}

class DrinksFactory extends Factory{
    @Override
    public Drinks getDrinks(String type) {
        if(type.equals("tea")){
            return new Tea();
        }else if(type.equals("cola")){
            return new Cola();
        }else {
            throw new RuntimeException("no such drinks");
        }
    }

    @Override
    public Food getFood(String type) {
        return null;
    }
}

class FactoryProducer{
    public static Factory getFactory(String type){
        if(type.equals("food")){
            return new FoodFactory();
        }else if(type.equals("drinks")){
            return new DrinksFactory();
        }else {
            throw new RuntimeException("no such factory");
        }
    }
}


public class O1_Factory_Abstract_02 {
    public static void main(String[] args) {
        Factory  factory = FactoryProducer.getFactory("food");
        Food xiaomi = factory.getFood("xiaomi");
        xiaomi.eat();

        Factory drinksFactory = FactoryProducer.getFactory("drinks");
        Drinks drinks= drinksFactory.getDrinks("cola");
        drinks.drink();

    }
}
